<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="zh-cn" >
	<head>
		<meta charset="utf-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="description" content="DnsMasqWeb">
		<title>记录管理 - DnsMasqWeb</title>
         {% load static %}
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<link href="/static/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
		<link href="/static/css/dnsmasqweb.css" rel="stylesheet">
        <script src="/static/js/jquery-1.8.3.min.js"></script>
		<script src="/static/bootstrap/js/bootstrap.min.js"></script>
		<script src="/static/js/dnsmasqweb.js"></script>
	</head>
	<body>
		<div class="container">
			{% include "top_nav.html" %}
			<div class="row">
				{% include "nav.html" %}
				<div class="span10">
					<h4>
						记录管理
					</h4>
                    <div class="pull-right">
                    <select name="domain" id="domain" onchange="get_record()">
                        <option value="0">-- 请选择域名 --</option>
                        {% for d in domains %}
                        <option value="{{d.id}}">{{d.domain}}</option>
                        {% endfor %}
                    </select>　
                    </div>
                    <div class="pull-right">
                    {% if did > 0 %}
                    <input type="button" class="btn btn-warning" value="同步解析" onclick="rsync_dns('no');">
                    <input type="button" class="btn btn-primary" value="添加记录" onclick="show_add();">
                    {% endif %}
                    </div><br /><br />
					<table class="table table-bordered table-striped table-hover table-condensed">
						<thead>
							<tr>
								<th><input type="checkbox" id="checkbox_all"></th>
                                <th>主机记录</th>
                                <th>记录类型</th>
                                <th>记录值</th>
                                <th>MX优先级</th>
                                <th>创建时间</th>
                                <th>更新时间</th>
                                <th>备注</th>
                                <th>操作</th>
							</tr>
						</thead>
						<tbody>
                            <tr id="add_line" class="display_no">
								<td></td>
                                <td><input type="text" name="record" id="record"  class="input-small"></td>
                                <td>
                                    <select style="width: 88px;" name="type" id="type">
                                        <option value="A">A</option>
                                        <option value="CNAME">CNAME</option>
                                        <option value="MX">MX</option>
                                        <option value="TXT">TXT</option>
                                        <option value="NS">NS</option>
                                        <option value="AAAA">AAAA</option>
                                        <option value="SRV">SRV</option>
                                    </select>
                                </td>
                                <td><input type="text" name="value" id="value"  class="input-small"></td>
                                <td><input type="text" name="priority" id="priority" style="width: 28px;"></td>
                                <td>{{get_time}}</td>
                                <td>{{get_time}}</td>
                                <td><input type="text" name="comment" id="comment"  class="input-small"></td>
								<td>
                                    <a href="#" class="view-link" onclick="add_record();">保存</a> |
                                    <a href="#" class="view-link" onclick="cancel_add()">取消</a>
                                </td>
							</tr>
                            {% load set_var %}
                            {% for r in records %}
                            {% if r.status == 'yes' %}
							<tr id="{{r.id}}">
                            {% set st_msg = "暂停" %}
                            {% set fun_value = "no" %}
                            {% else %}
                            <tr id="{{r.id}}" class="error">
                            {% set st_msg = "开启" %}
                            {% set fun_value = "yes" %}
                            {% endif %}
								<td><input type="checkbox"> {{loop.index}}</td>
                                <td title="{{r.record}}.{{cur_domain.domain}}">{{r.record}}</td>
                                <td>{{r.type}}</td>
                                <td>{{r.value}}</td>
                                <td>{{r.priority}}</td>
                                <td>{{r.create_time}}</td>
                                <td>{{r.update_time}}</td>
                                <td>{{r.comment}}</td>
								<td>
                                    <a href="javascript:void(0);" onclick="to_edit({{r.id}})">编辑</a> |
                                    <a href="/public/api?module=record&fun=ch_status&id={{r.id}}&value={{fun_value}}&did={{did}}" onclick="return confirm('确认{{st_msg}}该记录？');">{{st_msg}}</a> |
                                    <a href="/public/api?module=record&fun=del&id={{r.id}}&did={{did}}" onclick="return confirm('确认删除该记录？');">删除</a>
                                </td>
							</tr>
                            {# 可编辑状态 #}
                            <tr id="edit_line_{{r.id}}" class="display_no">
                                <td><input type="checkbox"> {{loop.index}}</td>
                                <td><input type="text" class="input-small" value="{{r.record}}" name="record_{{r.id}}" id="record_{{r.id}}"></td>
                                <td>
                                    <select style="width: 88px;" name="type_{{r.id}}" id="type_{{r.id}}" >
                                        <option value="A">A</option>
                                        <option value="CNAME">CNAME</option>
                                        <option value="MX">MX</option>
                                        <option value="TXT">TXT</option>
                                        <option value="NS">NS</option>
                                        <option value="AAAA">AAAA</option>
                                        <option value="SRV">SRV</option>
                                    </select>
                                    <script type="text/javascript">$("#type_{{r.id}}").val("{{r.type}}");</script>
                                </td>
                                <td><input type="text" class="input-small" value="{{r.value}}" name="value_{{r.id}}" id="value_{{r.id}}"></td>
                                <td><input type="text" class="input-mini" value="{{r.priority}}" style="width: 28px;" name="priority_{{r.id}}" id="priority_{{r.id}}"></td>
                                <td>{{r.create_time}}</td>
                                <td>{{r.up_time}}</td>
                                <td><input type="text" class="input-small" value="{{r.comment}}" name="comment_{{r.id}}" id="comment_{{r.id}}"></td>
                                <td>
                                    <a href="javascript:void(0);" onclick="save_info({{r.id}});">保存</a> |
                                    <a href="javascript:void(0);" onclick="to_line({{r.id}})">取消</a>
                                </td>
                            </tr>
                            {% endfor %}
						</tbody>
					</table>
				</div>
			</div>
		</div>
<script type="text/javascript">
function add_record() {
                did = $("#domain").val();
                record = $("#record").val();
                type = $("#type").val();
                value = $("#value").val();
                priority = $("#priority").val();
                comment = $("#comment").val();
                if ( record == '') {
                    alert("请输入记录！");
                    $("#record").focus();
                    return false;
                };
                if ( value == '') {
                    alert("请输入记录值！");
                    $("#value").focus();
                    return false;
                };
                if ( type == 'MX' && priority == '' ) {
                    alert("请输入MX优先级！");
                    $("#priority").focus();
                    return false;
                };
                $.ajax({
                    type: "POST",
                    url: "/dns/record/",
                    data: { 'record':record, 'type':type, 'value':value, 'comment':comment, 'did':did, 'priority':priority },
                    dataType: "text",
                    success: function(msg){
                        if (msg == "1") {
                            alert("添加成功！");
                            window.location.href="/dns/record?did="+did;
                        } else if (msg == "2") {
                            alert("记录已存在！");
                            $("#record").focus();
                        } else {
                            alert("提示：添加记录失败！");
                        }
                        },
                    error:function(){
                        alert("提示：服务器内部错误！");
                        },
                    });
            };
            function show_add(){
                $("#add_line").removeClass("display_no");
                $("#record").focus();
            };
            function cancel_add(){
                $("#add_line").addClass("display_no");
            };
            function get_record(){
                did = $("#domain").val();
                location.href="/dns/record/?did="+did;
            };
            function rsync_dns(force){
                did = $("#domain").val();
                $.ajax({
                    type: "GET",
                    url: "/public/api",
                    data: { "module":"dnsmasq", "fun":"rsync", "id":did, "force":force },
                    dataType: "text",
                    success: function(msg){
                        if (msg == "0") {
                            alert("同步成功！");
                            return true;
                        } else if (msg == "1") {
                            alert("DNSmasq服务重启失败！");
                            return false;
                        } else if (msg == "2") {
                            alert("提示：域名不存在或已停用！");
                        } else {
                            alert("提示：同步解析失败！");
                        }
                        },
                    error:function(){
                        alert("提示：服务器内部错误！");
                        },
                    });
            };
            $("#domain").val({{did}});
            function to_edit(id){
                show_id = "#line_" + id;
                edit_id = "#edit_line_" + id;
                $(show_id).addClass("display_no");
                $(edit_id).removeClass("display_no");
            };
            function to_line(id){
                show_id = "#line_" + id;
                edit_id = "#edit_line_" + id;
                $(show_id).removeClass("display_no");
                $(edit_id).addClass("display_no");
            };
            /* 编辑保存记录 */
            function save_info(id){
                did = $("#domain").val();
                record = $("#record_"+id).val();
                type = $("#type_"+id).val();
                value= $("#value_"+id).val();
                priority = $("#priority_"+id).val();
                comment = $("#comment_"+id).val();
                $.ajax({
                    type: "POST",
                    url: "/dns/record/",
                    data: { "id":id, "did":did, "record":record, "value":value, "type":type, "priority":priority, "comment":comment, "fun":"edit" },
                    dataType: "text",
                    success: function(msg){
                        if (msg == "1") {
                            alert("修改成功！");
                            location.href="/dns/record/?did="+did;
                        } else {
                            alert("提示：修改失败！");
                            return false;
                        }
                        },
                    error:function(){
                        alert("提示：服务器内部错误！");
                        return false;
                        },
                    });
            };
        </script>
	</body>
</html>